package com.first.orange.controller;

import com.first.orange.beans.PageSupport;
import com.first.orange.beans.Product;
import com.first.orange.beans.UserLogin;
import com.first.orange.service.PersonService;
import com.first.orange.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * Created on 2021/7/5.
 * <p>
 * Author: wsw
 * <p>
 * Description:
 */
@Controller
public class SearchController {
    Integer userId;
    @Autowired
    private ProductService productService;

    @Autowired
    private PersonService personService;

    /**
     * @return : java.lang.String
     * @author : wsw
     * @date : 2021/7/8
     * @description :  去往搜索界面
     */
    @RequestMapping("/search")
    public String productList(@RequestParam(required = false) String searchMsg, Map<String, Object> map, HttpSession session) {

        if (null == session.getAttribute("user") || "".equals(session.getAttribute("user"))) {
            return "redirect:/doLogin";
        }
        UserLogin userLogin = (UserLogin) session.getAttribute("user");
        userId = userLogin.getUserId();

        map.put("brands", productService.queryProductBrand());
        map.put("kinds", productService.queryProductKind());
        map.put("searchMsg", searchMsg);
        return "home/search";
    }

    /**
     * @return : com.first.orange.beans.PageSupport
     * @author : wsw
     * @date : 2021/7/8
     * @description :  给前端返回查询的分页数据
     */
    @ResponseBody
    @RequestMapping("/searchCount")
    public PageSupport searchCount(@RequestParam("searchMsg") String searchMsg, @RequestParam(required = false) String brandId, @RequestParam(required = false) String kindId,@RequestParam Integer pageSize) {
        PageSupport pageSupport = new PageSupport();
        pageSupport.setPageSize(pageSize);
        pageSupport.setTotalCount(productService.queryProductCount(searchMsg, brandId, kindId));
        return pageSupport;
    }

    /**
     * @return : java.util.List<com.first.orange.beans.Product>
     * @author : wsw
     * @date : 2021/7/8
     * @description :  给前端返回根据需求查询的商品列表
     */
    @ResponseBody
    @RequestMapping("/searchByCategory")
    public List<Product> productListByCategory(@RequestParam("searchMsg") String searchMsg, @RequestParam(required = false) String brandId, @RequestParam(required = false) String kindId, @RequestParam int sortNum, @RequestParam int currPageNo,@RequestParam int pageSize) {
       // System.out.println(productService.queryProductListByCategory(searchMsg, brandId, kindId, sortNum, currPageNo));
        return productService.queryProductListByCategory(searchMsg, brandId, kindId, sortNum, currPageNo,pageSize);
    }

    /**
     * @return : java.lang.String
     * @author : wsw
     * @date : 2021/7/8
     * @description :  获取商品口味、包装列表，根据商品id获取商品信息，跳转详情页
     */
    @RequestMapping("/toProductInfo")
    public String toproductInfo(@RequestParam String productId, Map<String, Object> map) {
        map.put("pictures", productService.queryProductPictureByProductId(productId));
        map.put("product", productService.queryProductById(productId));
        map.put("tastes", productService.queryProductTaste());
        map.put("packs", productService.queryProductPack());

       if(null!=productService.queryProductByuserIdAndProductId(userId,productId)){
           personService.deleteFootByuserIdAnd(productService.queryProductByuserIdAndProductId(userId,productId).getFootPrintId(),userId);
       }
        productService.addFootPrint(userId, productId);
        return "home/introduction";
    }
}
